package com.gxa.autoparts.admin.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.autoparts.admin.service.ArticleService;
import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.Article;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 廖泓宇
 * 文章管理
 * @date 2022/1/6 10:01
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @PostMapping("/addArticle")
    @ApiOperation("添加文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title",value = "文章标题",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "context",value = "文章内容",required = true,dataType = "String",paramType = "query")
    })
    public Map addArticle(String title,String context) throws BusinessException {
        Integer integer = articleService.insertArticle(title, context);
        JsonResult jsonResult = new JsonResult();
        if (integer > 0 ){
            jsonResult.setCode("200");
        }else {
            throw new BusinessException(ExceptionCode.ALTER_WRONG);
        }
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectAll")
    @ApiOperation("文章全查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,dataType = "Integer",paramType = "query")
    })
    public Map selectAll(Integer page, Integer size) throws IllegalAccessException {
        Article article = new Article();
        PageInfo<Article> articleServicePageInfo = articleService.selectByPage(page, size,article);
        List<Article> list = articleServicePageInfo.getList();
        List<Article> articles = articleService.selectAll();
        int size1 = articles.size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(list);
        jsonResult.put("count",size1);
        return jsonResult.getDataMap();
    }

    @PostMapping("/updateArticle")
    @ApiOperation("修改单个文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title",value = "文章标题",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "context",value = "文章内容",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "id",value = "文章id",required = true,dataType = "Long",paramType = "query")
    })
    public Map updateArticle(String title,String context,Long id) throws BusinessException {
        Article article = new Article();
        article.setTitle(title);
        article.setContext(context);
        article.setId(id);
        Integer integer = articleService.updateById(article);
        JsonResult jsonResult = new JsonResult();
        if (integer > 0){
            jsonResult.setCode("200");
        }else {
            throw new BusinessException(ExceptionCode.ALTER_WRONG);
        }
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteArticle")
    @ApiOperation("删除单个文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文章id",required = true,dataType = "Long",paramType = "query")
    })
    public Map deleteArticle(Long id) throws BusinessException {
        Integer integer = articleService.deleteById(id);
        JsonResult jsonResult = new JsonResult();
        if (integer > 0){
            jsonResult.setCode("200");
        }else {
            throw new BusinessException(ExceptionCode.NO_PERSON);
        }
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectById")
    @ApiOperation("按id查询文章信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文章id",required = true,dataType = "Long",paramType = "query")
    })
    public Map selectById(Long id){
        Article article = articleService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(article);
        return jsonResult.getDataMap();
    }





}
